package com.getpebble.android.framework.appmessage;

import android.util.Base64;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.appmessage.PebbleTuple.TupleType;
import com.getpebble.android.framework.appmessage.PebbleTuple.Width;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonPebbleDictionary extends PebbleDictionary {

    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType = new int[TupleType.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.BYTES.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.STRING.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.INT.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[TupleType.UINT.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public JsonPebbleDictionary(PebbleDictionary other) {
        super(other);
    }

    public String toJsonLegacyStyle() throws JSONException {
        JSONArray array = new JSONArray();
        for (PebbleTuple t : this.tuples.values()) {
            array.put(PebbleTuple.serializeTuple(t));
        }
        return array.toString();
    }

    public JSONObject toJson() {
        try {
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonSuperObject = super.toJson();
            jsonObject.put("instance", getClass().getSimpleName());
            String str = "contents";
            if (jsonSuperObject == null) {
                jsonSuperObject = "((null))";
            }
            jsonObject.put(str, jsonSuperObject);
            return jsonObject;
        } catch (Exception e) {
            return null;
        }
    }

    public static JsonPebbleDictionary fromJson(String jsonString) throws JSONException {
        JsonPebbleDictionary d = new JsonPebbleDictionary();
        JSONArray elements = new JSONArray(jsonString);
        int idx = 0;
        while (idx < elements.length()) {
            JSONObject o = elements.getJSONObject(idx);
            int key = o.getInt("key");
            TupleType type = (TupleType) PebbleTuple.TYPE_NAMES.get(o.getString("type"));
            Width width = (Width) PebbleTuple.WIDTH_MAP.get(Integer.valueOf(o.getInt("length")));
            if (type == null) {
                Trace.warning("JsonPebbleDictionary", "JsonPebbleDictionary: invalid type:" + o.getString("type"));
                throw new JSONException("invalid type: " + o.getString("type"));
            } else if (width == null) {
                Trace.warning("JsonPebbleDictionary", "JsonPebbleDictionary: invalid length:" + o.getInt("length"));
                throw new JSONException("invalid length: " + o.getInt("length"));
            } else {
                switch (AnonymousClass1.$SwitchMap$com$getpebble$android$framework$appmessage$PebbleTuple$TupleType[type.ordinal()]) {
                    case SMTPMessage.RETURN_FULL /*1*/:
                        d.addBytes(key, Base64.decode(o.getString("value"), 2));
                        break;
                    case SMTPMessage.RETURN_HDRS /*2*/:
                        d.addString(key, o.getString("value"));
                        break;
                    case ListInfo.INDETERMINATE /*3*/:
                        if (width != Width.BYTE) {
                            if (width != Width.SHORT) {
                                if (width != Width.WORD) {
                                    break;
                                }
                                d.addInt32(key, o.getInt("value"));
                                break;
                            }
                            d.addInt16(key, (short) o.getInt("value"));
                            break;
                        }
                        d.addInt8(key, (byte) o.getInt("value"));
                        break;
                    case SMTPMessage.NOTIFY_DELAY /*4*/:
                        if (width != Width.BYTE) {
                            if (width != Width.SHORT) {
                                if (width != Width.WORD) {
                                    break;
                                }
                                d.addUint32(key, o.getInt("value"));
                                break;
                            }
                            d.addUint16(key, (short) o.getInt("value"));
                            break;
                        }
                        d.addUint8(key, (byte) o.getInt("value"));
                        break;
                    default:
                        break;
                }
                idx++;
            }
        }
        return d;
    }
}
